package com.wenlong.agricultural.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wenlong.agricultural.pojo.UserInfo;
import com.wenlong.agricultural.service.UserService;
import com.wenlong.agricultural.utils.ResponseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 苏文龙
 * @version 1.0
 */
@Controller()
@RequestMapping("/agricultural")
public class LoginAndRegist {

    @Autowired
    private UserService userService;

    @GetMapping("/getVailUsername")
    @ResponseBody
    public ResponseInfo getVailUsername(@RequestParam("username") String username){
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        UserInfo user = userService.getOne(queryWrapper);
        if (user == null) {
            return ResponseInfo.ok()._massage("用户名合法");
        }else {
            return ResponseInfo.error()._massage("用户名已经存在");
        }
    }

    @RequestMapping("/loginInfo")
    @ResponseBody
    public ResponseInfo loginInfo(@RequestParam("username") String username,
                                  @RequestParam("password") String password,
                                  HttpServletRequest request){
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        UserInfo user = userService.getOne(queryWrapper);
        if(user == null) {
            return ResponseInfo.error()._massage("账号错误");
        }else if (!user.getPassword().equals(password)){
            return ResponseInfo.error()._massage("密码错误");
        }else {
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(-1);
            session.setAttribute("user", user);
            return ResponseInfo.ok();
        }
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/success")
    @ResponseBody
    public ResponseInfo success(UserInfo user) {
        if (user.getUsername() != null && user.getPassword() != null) {
            userService.save(user);
            return ResponseInfo.ok()._massage("恭喜你,注册成功");
//            return "redirect:/agricultural/login";
        }else {
            return ResponseInfo.error()._massage("用户名或密码错误");
//            return "redirect:/agricultural/register";
        }
    }

    //    @RequestMapping("/success")
    public String testUrl() {
        return "success";
    }

}
